Public Class ProductionStatusReport
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents ddlBONo As System.Web.UI.WebControls.DropDownList
    Protected WithEvents lblCurrentDate As System.Web.UI.WebControls.Label
    Protected WithEvents lblCustomerName As System.Web.UI.WebControls.Label
    Protected WithEvents lblWeek As System.Web.UI.WebControls.Label
    Protected WithEvents litReport As System.Web.UI.WebControls.Literal
    Protected WithEvents lblSupplierCode As System.Web.UI.WebControls.Label
    Protected WithEvents lblTotalItem As System.Web.UI.WebControls.Label
    Protected WithEvents lblCustomerReportList As System.Web.UI.WebControls.Label
    Protected WithEvents hrefCustomerReportList As System.Web.UI.HtmlControls.HtmlAnchor

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Protected WithEvents Header1 As Header
    Protected WithEvents rflc As ReportFunctionsLinkCtrl
    Public Const PRODUCTION_STATUS_REPORT_SESSION_NAME As String = "MIMS_WRS.Web.ProductionStatusReport"

#Region "   Properties"
    Protected ReadOnly Property CustomerID() As Integer
        Get
            Dim cid As Integer

            If Not Security.IsCustomer Then
                Try
                    cid = CInt(Request.QueryString("cid"))
                Catch ex As Exception
                    cid = -1
                End Try
            Else
                cid = Security.GetCurrentUser.UsIdPk
            End If

            If Not cid > 0 Then
                Throw New ApplicationException("Invalid or missing parameter")
            End If

            Return cid
        End Get
    End Property

    Protected ReadOnly Property Year() As Integer
        Get
            Try
                If IsNothing(Request.QueryString("y")) Then
                    Throw New Exception
                End If

                Dim y As Integer
                y = CInt(Request.QueryString("y"))

                If y < 0 Then
                    Throw New Exception
                End If

                Return y
            Catch ex As Exception
                Throw New ApplicationException("Invalid or missing parameter")
            End Try
        End Get
    End Property

    Protected ReadOnly Property Week() As Integer
        Get
            Try
                If IsNothing(Request.QueryString("w")) Then
                    Throw New Exception
                End If

                Dim w As Integer
                w = CInt(Request.QueryString("w"))

                If w < 1 Or w > 52 Then
                    Throw New Exception
                End If

                Return w
            Catch ex As Exception
                Throw New ApplicationException("Invalid or missing parameter")
            End Try
        End Get
    End Property

    Protected Property PageBO() As CProductionStatusReport
        Get
            Return CType(Session(Me.PRODUCTION_STATUS_REPORT_SESSION_NAME), CProductionStatusReport)
        End Get
        Set(ByVal Value As CProductionStatusReport)
            Session(Me.PRODUCTION_STATUS_REPORT_SESSION_NAME) = Value
        End Set
    End Property
#End Region

    Private Function GetTotalItemNumber(ByVal arrayOrderID As ArrayList) As Integer
        Dim total As Integer
        total = 0

        Dim orderIDAsString As String
        For Each orderIDAsString In arrayOrderID
            Dim orderID As Integer
            orderID = CInt(orderIDAsString)

            Dim order As New COrder
            order.Load(orderID)
            total = total + order.OrderItems.Count
        Next

        Return total
    End Function

    Private Function GetArrayOrderID() As ArrayList
        Dim arrayOrderID As New ArrayList
        Dim item As ListItem
        For Each item In Me.ddlBONo.Items
            If (Not item.Value.Equals("0")) Then
                arrayOrderID.Add(item.Value)
            End If
        Next

        Return arrayOrderID
    End Function

    Private Sub BindGrid()
        Dim orderID As Integer
        orderID = CInt(Me.ddlBONo.SelectedItem.Value)
        If (orderID > 0) Then
            Dim objO As New COrder
            objO.Load(CInt(Me.ddlBONo.SelectedItem.Value))

            Me.lblCurrentDate.Text = FormatDate(objO.OrderDate)
            Me.lblSupplierCode.Text = objO.SupplierCode
            Me.lblTotalItem.Text = objO.OrderItems.Count.ToString()
        Else
            'This array of order IDs will used when the report is generated.
            PageBO.OrderIDs = GetArrayOrderID()

            Me.lblCurrentDate.Text = "N/A"
            Me.lblSupplierCode.Text = "N/A"
            Me.lblTotalItem.Text = GetTotalItemNumber(PageBO.OrderIDs).ToString()
        End If


        PageBO.BoID = CInt(Me.ddlBONo.SelectedItem.Value)
        PageBO.Year = Me.Year
        PageBO.Week = Me.Week
        PageBO.CustomerID = Me.CustomerID

        'There are a couple of values which we save here will be used for later 
        'in the Print function.
        PageBO.CustomerName = Me.lblCustomerName.Text
        PageBO.OrderNumberAsDisplay = Me.ddlBONo.SelectedItem.Text
        PageBO.SupplierCode = Me.lblSupplierCode.Text
        PageBO.OrderDateAsDisplay = Me.lblCurrentDate.Text
        PageBO.TotalItemAsDisplay = Me.lblTotalItem.Text

        PageBO.Output = Report.EReportOutputType.Screen
        PageBO.RefreshReport()

        Me.litReport.Text = PageBO.GetReportHTML

    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then
            Response.Cache.SetCacheability(HttpCacheability.NoCache)

            Header1.NavigationBar.Add("Back to Weekly Report List", AppRoot() & "/Customer/WeeklyReportList.aspx?cid=" & Me.CustomerID.ToString)

            If Security.IsCustomer Then
                Header1.MenuIndex = EMenuIndex.CustomerWeeklyReport
                hrefCustomerReportList.Visible = False
            Else
                lblCustomerReportList.Visible = False
                Header1.MenuIndex = EMenuIndex.InternalReport
                Header1.NavigationBar.Add("Back to Customer Reports List", AppRoot() & "/Internal/Reports.aspx")
            End If

            Me.lblCustomerName.Text = CUser.GetName(Me.CustomerID)

            Me.lblWeek.Text = String.Format("Week: {0} / Year: {1}", Me.Week, Me.Year)


            Dim dt As DataTable
            dt = COrder.GetAllBulkOrderAndIndependentPO(Me.CustomerID)

            Me.ddlBONo.DataSource = dt
            Me.ddlBONo.DataTextField = "ORDER_NUMBER"
            Me.ddlBONo.DataValueField = "ORDER_ID_PK"
            Me.ddlBONo.DataBind()


            If dt.Rows.Count > 0 Then
                Dim obj As New CProductionStatusReport(CInt(Me.ddlBONo.SelectedItem.Value), Me.Year, Me.Week, Report.EReportOutputType.Screen)
                Me.PageBO = obj
                Me.BindGrid()

                'Allow the end user to view all orders details.
                Dim item As New ListItem("All ", "0")
                Me.ddlBONo.Items.Add(item)
            Else
                Me.rflc.Visible = False
                Me.ddlBONo.Items.Add("--There is no order--")
            End If

        End If
    End Sub

    Private Sub ddlBONo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlBONo.SelectedIndexChanged
        Me.BindGrid()
    End Sub

    Private Sub rflc_ExportClick() Handles rflc.ExportClick
        PageBO.Output = Report.EReportOutputType.File
        PageBO.RefreshReport()
        'output to user
        Response.Clear()
        Response.ContentType = "application/msexcel"
        Response.AddHeader("content-disposition", "attachment; filename=""" & "PSR" & Me.Week & Me.Year & "_" & GetRandomFileName() & ".xls""")
        Response.Write(PageBO.GetReportHTML)
        Response.End()
    End Sub


    Private Sub rflc_DownloadCSVClick() Handles rflc.DownloadCSVClick
        PageBO.Output = Report.EReportOutputType.FileCSV
        PageBO.RefreshReport()
        'output to user
        Response.Clear()
        Response.ContentType = "application/msexcel"
        Response.AddHeader("content-disposition", "attachment; filename=""" & "PSR" & Me.Week & Me.Year & "_" & GetRandomFileName() & ".csv""")
        Response.Write(PageBO.GetReportHTML)
        Response.End()
    End Sub

    Private Sub rflc_DownloadCSV2Click() Handles rflc.DownloadCSV2Click
        PageBO.Output = Report.EReportOutputType.FileCSV2
        PageBO.RefreshReport()
        'output to user
        Response.Clear()
        Response.ContentType = "application/msexcel"
        Response.AddHeader("content-disposition", "attachment; filename=""" & "PSR" & Me.Week & Me.Year & "_" & GetRandomFileName() & ".csv""")
        Response.Write(PageBO.GetReportHTML)
        Response.End()
    End Sub
End Class
